﻿using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PropertyChanged;
namespace AgileLogistics.View.Home
{[AddINotifyPropertyChangedInterface]
    /// <summary>
    /// Home.xaml 的交互逻辑
    /// </summary>
    public partial class Home : Page
    {
        ViewModel.HomeViewModel vm;
        Model.DBContext dBContext = new Model.DBContext();
        Model.Repository<Model.Notice> RepNotice = new Model.Repository<Model.Notice>();
        public Home()
        {
            InitializeComponent();

            vm = DataContext as ViewModel.HomeViewModel;
            //vm.NoticeList = RepNotice.GetList();
            vm.NoticeUsersList = dBContext.DB.Queryable<Model.NoticeUsers>().Where(nu => nu.UserId == vm.LoginUser.Id  && nu.IsRead ==false ).ToList();

            vm.HistoryNoticeUsersList = dBContext.DB.Queryable<Model.NoticeUsers>().Where(nu => nu.UserId == vm.LoginUser.Id && nu.IsRead == true).ToList();
            //获取数据
            vm.MenusCount = dBContext.DB.Queryable<Model.Menus>().Count();
            vm.PollCount = dBContext.DB.Queryable<Model.PollItem>().Sum(pi => pi.PollNum);
            vm.UserCount = dBContext.DB.Queryable<Model.Users>().Count();
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if( vm ==null || vm.CurrNoticeUsers == null)
            {
                return;
            }
            NoticeContent win = new NoticeContent();
            win.ShowDialog();
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            vm.CurrNoticeUsers.IsRead = true;
            dBContext.DB.Updateable<Model.NoticeUsers>(vm.CurrNoticeUsers).ExecuteCommand();
            vm.NoticeUsersList = dBContext.DB.Queryable<Model.NoticeUsers>().Where(nu => nu.UserId == vm.LoginUser.Id && nu.IsRead == false).ToList();

        }
    }
}
